#include "xmppcore/saslstreamfeature.h"
#include "xmppcore/constants.h"

namespace yoohoo_xmppcore {

SaslStreamFeature::SaslStreamFeature() : StreamFeature(XMPP_MECHANISMS_NAME, XMPP_SASL_NAMESPACE) {
}

QList<QString> SaslStreamFeature::mechanisms() {
	return _mechanisms;
}

bool SaslStreamFeature::fromXml(QXmlStreamReader& reader) {
	while (true) {
		reader.readNext();
		if (reader.hasError())
			return false;

		if (reader.isEndElement()) {
			return true;
		}

		if (reader.isStartElement() && reader.prefix().toString() == "" &&
				reader.name().toString() == "mechanism") {
			if (!readMechanism(reader))
				return false;
		}
	}
}

bool SaslStreamFeature::readMechanism(QXmlStreamReader& reader) {
	reader.readNext();
	if (reader.hasError())
		return false;

	if (reader.isCharacters()) {
		_mechanisms.push_back(reader.text().toString());
	} else {
		return false;
	}

	reader.readNext();
	if (reader.hasError())
		return false;

	if (reader.isEndElement()) {
		return true;
	}

	return false;
}

}
